﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DMSMT.NetUtilityLib;
using Newtonsoft.Json.Linq;

namespace DMSMT.Tool
{
    public partial class ServerVerFrm : Form
    {
        public ServerVerFrm()
        {
            InitializeComponent();
        }

        private string Dosearch(string ip)
        {
            string url = "http://"+ip+"/DMS/system/loginAction!login2.action";
            string param = "jsonParams={\"loginName\":\"root\",\"password\":\"123456\"}";
            
            HttpUtility hu = new HttpUtility();
            if (url == "")
            {
                return "请求地址不能为空";
            }
            string retString = hu.HttpPost(url, param);

            JObject o = JObject.Parse(retString);
            //Console.WriteLine(o["Tbody"].ToString());
            //Log.LogText(o["msg"].ToString());
            string msg = o["msg"].ToString();
            if (string.IsNullOrEmpty(msg))
            {
                return "连接服务器失败";
            }
            else
            {
                if (msg.Equals("success"))
                {
                    url = "http://"+ip+"/DMS/system/menuAction!toMenuAction.action";
                    retString = hu.HttpGet(url);

                    int index = retString.IndexOf("版本号:");
                    int indexE = retString.IndexOf("'", index);
                    string ver = retString.Substring(index, indexE - index);
                    return ver;

                }
                else
                {
                    //Log.LogText(retString);
                    return msg;
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //string url = "http://localhost:1400/DMS/system/loginAction!login2.action";
            //string param = "jsonParams={\"loginName\":\"pandl\",\"password\":\"123456\"}";
            //string ip = "172.15.102.92:8080";
            //string res = Dosearch(ip);
            //Log.LogText(ip +"\t"+ res);

            string[] ips = rtbIP.Lines;
            if (ips.Length == 0)
            {
                MessageBox.Show("请输入IP列表");
                return;
            }

            foreach (string ip in ips)
            {
                string ipstr = ip.Trim();
                if (!string.IsNullOrEmpty(ipstr))
                {
                    try
                    {
                        string res = Dosearch(ipstr + ":8080");
                        Log.LogText(ipstr + "\t" + res);
                    }
                    catch(Exception ex)
                    {
                        Log.LogError(ipstr + "\t" + ex.Message);
                    }
                }
            }

          
        }
    }
}
